package logic

import (
	"context"
	"eight-dimensional-mall/goods_srv/goods"
	"eight-dimensional-mall/goods_srv/internal/svc"
	"eight-dimensional-mall/goods_srv/model/mysql"
	"eight-dimensional-mall/goods_srv/model/redis"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *goods.CreateGoodsReq) (*goods.CreateGoodsResp, error) {
	g := mysql.Goods{
		GoodsTitle: in.GoodsTitle,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsStock: in.GoodsStock,
		GoodsImg:   in.GoodsImg,
	}
	err := g.CreateGoods()
	if err != nil {
		logx.Error("创建商品失败", err)
		return nil, err
	}
	redis.SyncGoodsStock(int(g.ID), int(g.GoodsStock))
	return &goods.CreateGoodsResp{
		GoodsId: int64(g.ID),
	}, nil
}
